package com.oceansoft.mobile.biz.admin.service;

import static com.oceansoft.mobile.common.Constant.DB_OPER_ONE_SUCC;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.oceansoft.mobile.biz.admin.entity.Auth;
import com.oceansoft.mobile.biz.admin.entity.BootMenu;
import com.oceansoft.mobile.biz.admin.entity.Profile;
import com.oceansoft.mobile.biz.admin.entity.SysUser;
import com.oceansoft.mobile.biz.api.entity.AuthUser;
import com.oceansoft.mobile.common.base.entity.Result;
import com.oceansoft.mobile.common.base.entity.SelectBox;
import com.oceansoft.mobile.common.dao.ICommonDao;
import com.oceansoft.mobile.weixin.util.MD5Util;

@Service
public class SysUserServiceImpl implements ISysUserService {

	@Resource
	private ICommonDao commonDao;

	public Result loginAuthen(Profile profile) {
		List<SysUser> list = new ArrayList<SysUser>();
		try {
			list = commonDao
					.loadList(
							"SELECT t.*, t2.company_name company_name FROM T_SYS_USER T LEFT JOIN t_merchant t2 ON t.merchant_id=t2.sid  WHERE T.LOGIN_ID = ? AND T.U_PWD = ?",
							SysUser.getLoginAuthRowMapper(), profile.getLoginId(),
							MD5Util.MD5(profile.getPassword()));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (list.size() > 0) {
			SysUser _u = list.get(0);
			_u.setU_pwd("");
			return _u.getStatus() == 1 ? new Result(_u) : new Result(
					Result.FAILURE, "当前账户不可用，请与管理员联系");
		}

		return new Result(Result.FAILURE, "用户名或密码错误");
	}

	public List<Auth> loadAuth(long auth_id) {
		List<Auth> authList = new ArrayList<Auth>();
		authList = commonDao.loadList(
				"select * from t_sys_auth where auth_id=?",
				Auth.getRowMapper(), auth_id);
		return authList;
	}

	public SysUser loadSysUserById(long loginId) {
		return commonDao.loadOne(
				"SELECT * FROM T_SYS_USER T WHERE T.LOGIN_ID = ?",
				SysUser.getLoginAuthRowMapper(), loginId);
	}

	public Result updatePassword(long id, String origiEncodingStr,
			String pwdEncodingStr) {
		int count = commonDao
				.getCount(
						"SELECT COUNT(*) FROM t_sys_user T WHERE T.id = ? AND T.u_pwd = ?",
						id, origiEncodingStr);
		if (0 == count) {
			return new Result(Result.FAILURE, "原始密码不正确");
		}
		int rt = commonDao.updateOne(
				"UPDATE t_sys_user T SET T.u_pwd = ? WHERE T.ID = ?",
				pwdEncodingStr, id);
		if (DB_OPER_ONE_SUCC == rt)
			return new Result();
		return new Result(Result.FAILURE, "密码更新失败");
	}

	@Override
	public SysUser loadSysUserByMerchantId(long merchant_id) {
		return commonDao
				.loadOne(
						"SELECT t.*, t2.company_name company_name FROM T_SYS_USER T LEFT JOIN t_merchant t2 ON t.merchant_id=t2.sid  WHERE T.merchant_id = ? and t.role_id=1",
						SysUser.getLoginAuthRowMapper(), merchant_id);
	}

	public List<BootMenu> listFristMenu() {
		List<BootMenu> menuList = commonDao
				.loadList(
						"select * from wx_menu T where T.parentid IS NULL and T.wx_openid = ?",
						BootMenu.getRowMapper(), "gh_1f31c32f42cd");
		return menuList;
	}

	public List<BootMenu> listSecondMenu(long sid) {
		List<BootMenu> menuList = commonDao.loadList(
				"select * from wx_menu T where T.parentid = ? ",
				BootMenu.getRowMapper(), sid);
		return menuList;
	}

	@Override
	public BootMenu editeMenu(long sid) {
		return commonDao.loadOne("select * from wx_menu T where T.sid = ?",
				BootMenu.getRowMapper(), sid);
	}

	@Override
	public Result saveOrEditeMenu(BootMenu bootMenu) {
		if (bootMenu.getSid() == 0) {
			bootMenu.setSid(0);
			commonDao
					.saveOne(
							"INSERT INTO wx_menu ( parentid,m_name,m_type,m_url,wx_openid) VALUES(:parentid,:m_name,:m_type,:m_url,:wx_openid)",
							bootMenu);
		} else {
			commonDao
					.updateOne(
							"UPDATE wx_menu T SET  t.m_name=:m_name,t.m_type=:m_type,t.m_url=:m_url  WHERE T.SID = :sid",
							bootMenu);
		}

		return new Result();
	}

	public Result deleteMenuById(long sid) {
		int _num = commonDao.getCount(
				"SELECT COUNT(*) FROM wx_menu T WHERE T.sid = ?", sid);
		if (_num == 0) {
			return new Result(Result.FAILURE, "已经不存在了");
		} else {
			int rt = commonDao.deleteOne("DELETE FROM wx_menu WHERE SID = ?",
					sid);
			if (rt > 0)
				return new Result();
		}
		return new Result(Result.FAILURE, "删除不成功");
	}

	public List<SelectBox> boxList(String description) {
		return commonDao.loadList(
				"SELECT * FROM tool_selectBox T WHERE T.description = ?",
				SelectBox.getRowMapper(), description);
	}

	@Override
	public void saveWeChatUser(AuthUser user) {
		commonDao
				.updateOne(
						"INSERT INTO t_api_user_auth ( openId,userId,updateTime) VALUES('"+user.getOpenId()+"','"+user.getUserId()+"',NOW())",
						user);
	}
}
